﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class GalleryButtonControl : UserControl, IControlRulable
    {
        private GalleryButton innerGalleryButton;

        public GalleryButton InnerGalleryButton
        {
            get { return innerGalleryButton; }
            set { innerGalleryButton = value; }
        }
        
        public GalleryButtonControl()
        {
            InitializeComponent();
            this.ecCommandType.Values = Enum.GetValues(typeof(Enumerations.CommandType));
            this.ecElementDimensions.Values = Enum.GetValues(typeof(Enumerations.ElementDimensions));
            this.innerGalleryButton = new GalleryButton();
            this.FillControls();
        }

        public GalleryButtonControl(GalleryButton galleryButton)
            : this()
        {
            this.innerGalleryButton = galleryButton;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcAlt.Value = this.innerGalleryButton.Alt;
                this.rcCommand.Value = this.innerGalleryButton.Command;
                this.rcCommandPreview.Value = this.innerGalleryButton.CommandPreview;
                this.rcCommandPreviewRevert.Value = this.innerGalleryButton.CommandRevert;
                this.ecCommandType.SelectedValue = this.innerGalleryButton.CommandType.ToString();
                this.strCommandValueId.Value = this.innerGalleryButton.CommandValueId;
                this.ecElementDimensions.SelectedValue = this.innerGalleryButton.ElementDimensions.ToString();
                this.strId.Value = this.innerGalleryButton.Id;
                this.rcImage.Value = this.innerGalleryButton.Image;
                this.strImageCss.Value = this.innerGalleryButton.ImageClass;
                this.icImageLeft.Value = this.innerGalleryButton.ImageLeft;
                this.icImageTop.Value = this.innerGalleryButton.ImageTop;
                this.strInnerHtml.Value = this.innerGalleryButton.InnerHTML;
                this.strMenuItemId.Value = this.innerGalleryButton.MenuItemId;
                this.strQueryCommand.Value = this.innerGalleryButton.QueryCommand;
                this.integerControlSequence.Value = this.innerGalleryButton.Sequence;
                this.stringControlTemplateAlias.Value = this.innerGalleryButton.TemplateAlias;
                this.referencedControlToolTipDescription.Value = this.innerGalleryButton.ToolTipDescription;
                this.stringControlToolTipHelpKeyWord.Value = this.innerGalleryButton.ToolTipHelpKeyWord;
                this.referencedControlToolTipImage32by32.Value = this.innerGalleryButton.ToolTipImage32by32;
                this.referencedControlToolTipImage32by32Class.Value = this.innerGalleryButton.ToolTipImage32by32Class;
                this.integerControlToolTipImage32by32Left.Value = this.innerGalleryButton.ToolTipImage32by32Left;
                this.integerControlToolTipImage32by32Top.Value = this.innerGalleryButton.ToolTipImage32by32Top;
                this.stringControlToolTipShortcutKey.Value = this.innerGalleryButton.ToolTipShortcutKey;
                this.referencedControlToolTipTitle.Value = this.innerGalleryButton.ToolTipTitle;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerGalleryButton.Alt = this.rcAlt.Value;
                    this.innerGalleryButton.Command = this.rcCommand.Value;
                    this.innerGalleryButton.CommandPreview = this.rcCommandPreview.Value;
                    this.innerGalleryButton.CommandRevert = this.rcCommandPreviewRevert.Value;
                    this.innerGalleryButton.CommandType = (Enumerations.CommandType)Enum.Parse(typeof(Enumerations.CommandType), this.ecCommandType.SelectedValue);
                    this.innerGalleryButton.CommandValueId = this.strCommandValueId.Value;
                    this.innerGalleryButton.ElementDimensions = (Enumerations.ElementDimensions)Enum.Parse(typeof(Enumerations.ElementDimensions), this.ecElementDimensions.SelectedValue);
                    this.innerGalleryButton.Id = this.strId.Value;
                    this.innerGalleryButton.Image = this.rcImage.Value;
                    this.innerGalleryButton.ImageClass = this.strImageCss.Value;
                    this.innerGalleryButton.ImageLeft = this.icImageLeft.Value;
                    this.innerGalleryButton.ImageTop = this.icImageTop.Value;
                    this.innerGalleryButton.InnerHTML = this.strInnerHtml.Value;
                    this.innerGalleryButton.MenuItemId = this.strMenuItemId.Value;
                    this.innerGalleryButton.QueryCommand = this.strQueryCommand.Value;
                    this.innerGalleryButton.Sequence = this.integerControlSequence.Value;
                    this.innerGalleryButton.TemplateAlias = this.stringControlTemplateAlias.Value;
                    this.innerGalleryButton.ToolTipDescription = this.referencedControlToolTipDescription.Value;
                    this.innerGalleryButton.ToolTipHelpKeyWord = this.stringControlToolTipHelpKeyWord.Value;
                    this.innerGalleryButton.ToolTipImage32by32 = this.referencedControlToolTipImage32by32.Value;
                    this.innerGalleryButton.ToolTipImage32by32Class = this.referencedControlToolTipImage32by32Class.Value;
                    this.innerGalleryButton.ToolTipImage32by32Left = this.integerControlToolTipImage32by32Left.Value;
                    this.innerGalleryButton.ToolTipImage32by32Top = this.integerControlToolTipImage32by32Top.Value;
                    this.innerGalleryButton.ToolTipShortcutKey = this.stringControlToolTipShortcutKey.Value;
                    this.innerGalleryButton.ToolTipTitle = this.referencedControlToolTipTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GalleryButtonControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerGalleryButton.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
